using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Diagnostics;

public class C_FollowTarget : C_Base
{
    public bool C_IsControledByMouse = true;
    public bool C_IsFaceRight = true;

    private GameObject targetGO;
    private float angle;

    public virtual void SetTargetGO(GameObject go)
    {
        targetGO = go;
    }
    public virtual Vector3 GetTargetPoint()
    {
        if (C_IsControledByMouse)
        {
            return Util.GetMouseWorldPosition();
        }
        else if (targetGO != null)
        {
            return targetGO.transform.position;
        }
        else
        {
            // 不符合上述两种情况
            return Vector3.zero;
        }
    }
    public virtual float GetAngle()
    {
        if (!C_IsControledByMouse && targetGO == null)
        {
            angle = 0;
        }
        else
        {
            Vector3 screenPos = GetTargetPoint();
            Vector3 dir = (screenPos - transform.position).normalized;
            angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
        }
        return angle;
    }
    /// <summary>
    /// 跟随Target
    /// </summary>
    public virtual void Follow()
    {
        if (GameManager.Instance.curState == GameState.Gaming)
        {
            GetAngle();
            if (Mathf.Abs(angle) < 90)
            {
                C_IsFaceRight = true;
                transform.localRotation = Quaternion.Euler(0, 0, angle);
            }
            else if (Mathf.Abs(angle) > 90)
            {
                C_IsFaceRight = false;
                transform.localRotation = Quaternion.Euler(0, 0, 180 - angle);
            }
        }
    }
    private void Update()
    {
        Follow();
    }
}
